package com.bsh.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsh.api.Exception.ApiException;
import com.bsh.service.ApiMerchantService;
import com.bsh.utils.ApiMessage;

/**
 * 商户相关 api
 * 
 * @author jane
 * @since 0.0.1
 * 
 */
@Controller
@RequestMapping(value = "/api/v1/merchant", method = { RequestMethod.POST })
public class ApiMerchantController {
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	HttpSession session;
	@Autowired
	ApiMerchantService apiMerchantService;

	/**
	 * 商户上传
	 * 
	 * @param paramStr
	 * @param idcardA
	 * @param idcardB
	 * @param certificate
	 * @param imgCover
	 * @param showImg1
	 * @param showImg2
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/register")
	@ResponseBody
	public ApiMessage registerMerchant(@RequestParam(value = "paramStr", required = true) String paramStr,
			@RequestParam(value = "idcardA") MultipartFile idcardA,
			@RequestParam(value = "idcardB") MultipartFile idcardB,
			@RequestParam(value = "certificate") MultipartFile certificate,
			@RequestParam(value = "imgCover") MultipartFile imgCover,
			@RequestParam(value = "showImg1", required = false) MultipartFile showImg1,
			@RequestParam(value = "showImg2", required = false) MultipartFile showImg2) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("serv_ip", ApiUtils.getPicUrl(request));
			msg = apiMerchantService.registerMerchant(dto, idcardA, idcardB, certificate, imgCover, showImg1, showImg2);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}

	/**
	 * 商户上传url
	 * 
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/add")
	@ResponseBody
	public ApiMessage add(@RequestParam(value = "paramStr", required = true) String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("serv_ip", ApiUtils.getPicUrl(request));
			msg = apiMerchantService.registerMerchantForUrl(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}

	/**
	 * 商户类型列表
	 * 
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/category", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage merchantCategory() {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = new JSONObject();
			dto.put("serv_ip", ApiUtils.getPicUrl(request));
			msg = apiMerchantService.merchantCategory(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}

	/**
	 * 商户详情
	 * 
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/detail", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage merchantDetail(@RequestParam(value = "paramStr", required = true) String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("serv_ip", ApiUtils.getPicUrl(request));
			msg = apiMerchantService.merchantDetail(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

	}

	/**
	 * 修改商户详情
	 * 
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/detail/change", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage changeMerchantDetail(@RequestParam(value = "paramStr", required = true) String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("serv_ip", ApiUtils.getPicUrl(request));
			msg = apiMerchantService.changeMerchantDetail(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

	}

	/**
	 * h5商户列表
	 * 
	 * @param paramStr
	 * @return
	 */
	@RequestMapping(value = "/H5MerchantList")
	@ResponseBody
	public ApiMessage H5MerchantList(@RequestParam(value = "paramStr", required = true) String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("serv_ip", ApiUtils.getPicUrl(request));
			msg = apiMerchantService.H5MerchantList(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		}
		return msg;
	}

	/**
	 * h5热门商户列表
	 * 
	 * @param paramStr
	 * @return
	 */
	@RequestMapping(value = "/H5MerchantHotList")
	@ResponseBody
	public ApiMessage hotList(@RequestParam(value = "paramStr", required = true) String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("serv_ip", ApiUtils.getPicUrl(request));
			msg = apiMerchantService.H5MerchantHotList(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		}
		return msg;
	}
	/**
	 * h5热门商户列表
	 * 
	 * @param paramStr
	 * @return
	 */
	@RequestMapping(value = "/H5MerchantDetail")
	@ResponseBody
	public ApiMessage H5MerchantDetail(@RequestParam(value = "paramStr", required = true) String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("serv_ip", ApiUtils.getPicUrl(request));
			msg = apiMerchantService.H5MerchantDetail(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		}
		return msg;
	}
}
